/**
 * 开发者：acaiacai2046
 * 开发时间：2020年3月9日 
 */
define(['layer', 'lazyload'], function(Layer, Lazyload) {
    var _dh_url = window.location.hostname;
    if(_dh_url != 'daohe.qianbudai.com' && _dh_url != 'requirejs.qianbudai.com' && _dh_url != 'gz.qianbudai.com'){
        _dh_url = "tongji.daohemedia.com";
    }else{
        _dh_url = "daohe.qianbudai.com";
    }
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        },
        test:function (form, errors) {
            //表单提交
            $('#gotosave_bt').click(function(){
                var title = $('title').text();
                var plan = validatoroptions.getUrlParam('plan');//

                var pathname = window.location.pathname;
                var parent_url = window.parent.document.referrer
                var name = $('input[name="name"]').val();
                var phone = $('input[name="tele"]').val();
                var ret = /^1[3|4|5|6|7|8|9]\d{9}$/;
                if(name == ""){
                    layer.msg('请输入您的姓名', {icon: 2});
                    return false;
                }

                if(phone == "" || !ret.test(phone)){
                    if(!ret.test(phone)){
                        layer.msg('请输您的手机号', {icon: 2});
                    }
                    return false;
                }

                $.ajax({
                    url:'http://'+_dh_url+'/api/yuyue/index',
                    type:"POST",
                    dataType:"json",
                    data:{
                        title:title,
                        name:name,
                        phone:phone,
                        plan:plan,
                        state:1,//0=默认,1=看房,2=留言,3=团购,4=特价房,5=项目咨询
                        pathname:pathname,
                        parent_url: parent_url,
                        siteurl:_dh_url,
                        admin_id:4//广州
                    },
                    beforeSend: function() {
                        $('#gotosave_bt').text('提交中...');
                    },
                    error: function(request) {
                        layer.msg(request.responseText, {icon: 2});
                    },
                    success:function(data){
                        if(data.code != '1'){
                            layer.msg(data.error, {icon: 2});
                        }else{
                            layer.msg("提交成功，稍后置业顾问会跟您联系。", {icon: 1});
                            $('input[name="name"]').val('');
                            $('input[name="tele"]').val('');
                        }
                        $('#gotosave_bt').text('立即报名');
                    }
                });

            });
        },
        getUrlParam:function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        },
        bdHeight:function () {
            $(".banner").each(function(i){
                var img=$(this);
                var url=$(this).css('backgroundImage');
                s = url.match(/url\((.*?)\)/);
                url = i ? s[1] : $(this).data('src');
                console.log(url);
                if(url[0]=="\""){url = url.slice(1,-1)}
                $("<img/>").attr("src", url).load(function() {
                    realWidth = this.width;
                    realHeight = this.height;
                    console.log(realHeight);
                    //如果真实的宽度大于浏览器的宽度就按照100%显示
                    img.css("height",realHeight+"px");
                });
            });
        }
    };
    var Controller = {
        index: function () {
            $(".banner, .imgShow").lazyload({
                event : "click"
            });

            $(".bm-vip").html(hotline.replace(',','转'));

            $("#baoming-small").click(function () {
                $("#phone").hide();
                $("#baoming-big").show();
            });

            $("#close").click(function () {
                $("#phone").show();
                $("#baoming-big").hide();
            });
            validatoroptions.test();
            validatoroptions.bdHeight(0);
            window.onscroll = function(){
                validatoroptions.bdHeight(1);
            }
        }
    };
    return Controller;
});